package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.TicketBusiness;
import com.ruoyi.system.domain.TicketCustomer;
import com.ruoyi.system.enums.BusType;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TicketReceiptMapper;
import com.ruoyi.system.domain.TicketReceipt;

import javax.validation.constraints.Size;

/**
 * 收据信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-15
 */
@Service
public class TicketReceiptServiceImpl implements ITicketReceiptService
{
    @Autowired
    private TicketReceiptMapper ticketReceiptMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private TicketCommonService commonService;

    @Autowired
    private ITicketBusinessService businessService;

    @Autowired
    private ITicketCustomerService customerService;

    /**
     * 查询收据信息
     *
     * @param receiptNo 收据信息主键
     * @return 收据信息
     */
    @Override
    public TicketReceipt selectTicketReceiptByReceiptNo(String receiptNo)
    {
        return ticketReceiptMapper.selectTicketReceiptByReceiptNo(receiptNo);
    }

    /**
     * 查询收据信息列表
     *
     * @param ticketReceipt 收据信息
     * @return 收据信息
     */
    @Override
    public List<TicketReceipt> selectTicketReceiptList(TicketReceipt ticketReceipt)
    {
        List<TicketReceipt> ticketReceipts = ticketReceiptMapper.selectTicketReceiptList(ticketReceipt);
        //判断当前登录人是否是商户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        SysUser sysUser = userService.selectUserById(userId);
        @Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符") String nickName = sysUser.getNickName();
        if(commonService.isBusiness()){
            TicketBusiness business = businessService.selectTicketBusinessByBusinessName(nickName);
            String businessNo = business.getBusinessNo();
            ticketReceipts= ticketReceipts
                    .stream()
                    .filter(p->p.getReceiptBusType().equals(BusType.merchant.name()))
                    .filter(p->p.getReceiptName().split("@")[0].equals(businessNo))
                    .collect(Collectors.toList());
            //判断是否是用户
        }else if(commonService.isCustomer()) {
            TicketCustomer ticketCustomer = customerService.selectTicketCustomerByCustomerName(nickName);
            String customerNo = ticketCustomer.getCustomerNo();
            ticketReceipts= ticketReceipts
                    .stream()
                    .filter(p->p.getReceiptBusType().equals(BusType.person.name()))
                    .filter(p->p.getReceiptName().split("@")[1].replaceAll("收据单","").equals(customerNo))
                    .collect(Collectors.toList());

        }
        return ticketReceipts;
    }

    /**
     * 新增收据信息
     *
     * @param ticketReceipt 收据信息
     * @return 结果
     */
    @Override
    public int insertTicketReceipt(TicketReceipt ticketReceipt)
    {
        if(StringUtils.isEmpty(ticketReceipt.getReceiptNo())){
            ticketReceipt.setReceiptNo(UUID.randomUUID().toString());
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ticketReceipt.setCreateBy(loginUser.getUsername());
        ticketReceipt.setCreateTime(DateUtils.getNowDate());
        return ticketReceiptMapper.insertTicketReceipt(ticketReceipt);
    }

    /**
     * 修改收据信息
     *
     * @param ticketReceipt 收据信息
     * @return 结果
     */
    @Override
    public int updateTicketReceipt(TicketReceipt ticketReceipt)
    {
        ticketReceipt.setUpdateTime(DateUtils.getNowDate());
        return ticketReceiptMapper.updateTicketReceipt(ticketReceipt);
    }

    /**
     * 批量删除收据信息
     *
     * @param receiptNos 需要删除的收据信息主键
     * @return 结果
     */
    @Override
    public int deleteTicketReceiptByReceiptNos(String[] receiptNos)
    {
        return ticketReceiptMapper.deleteTicketReceiptByReceiptNos(receiptNos);
    }

    /**
     * 删除收据信息信息
     *
     * @param receiptNo 收据信息主键
     * @return 结果
     */
    @Override
    public int deleteTicketReceiptByReceiptNo(String receiptNo)
    {
        return ticketReceiptMapper.deleteTicketReceiptByReceiptNo(receiptNo);
    }
}
